package com.ruoyi.system.interAttr.attr;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.interAttr.domain.GamesRes;
import com.ruoyi.system.interAttr.domain.InterAttrRes;
import com.ruoyi.system.interAttr.domain.InterServerRes;
import com.ruoyi.system.model.domain.SalePartner;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * 淘号号 属性匹配
 */
@Slf4j
@Service
public class LXYAttr {

    public  List<GamesRes> getGames(SalePartner salePartner) {
        String[] games = "王者荣耀,王者国服".split(",");
        List<GamesRes> gamesResList = new ArrayList<>();
        for(String s : games) {
            GamesRes gamesRes = new GamesRes();
            gamesRes.setId(s);
            gamesRes.setGameName(s);
            gamesResList.add(gamesRes);
        }
        return gamesResList;
    }

    public  List<InterServerRes> getServer(String gameId, SalePartner salePartner) {
        List<InterServerRes> interServerRes = new ArrayList<>();
        String[] string = "6=QQ苹果，4=QQ安卓，7=微信苹果，5=微信安卓".split("，");
        if(gameId.equals("王者国服")){
            string = "16=安卓（QQ区），17=安卓（微信区），18=苹果（QQ区），19=苹果（微信区），20=抢先服（安卓）".split("，");
        }
        for(String s:string){
            InterServerRes res = new InterServerRes();
            res.setId(s.split("=")[0]);
            res.setName(s.split("=")[1]);
            interServerRes.add(res);
        }
        return interServerRes;
    }

    public  List<InterAttrRes> getPropety(String gameId, String type, SalePartner salePartner) {
        String[] strings = null;
        if(gameId.equals("王者国服")){
            strings = "内测,荣耀属性,星元皮肤,稀有皮肤,史诗传说,发育路国服,对抗路国服,游走路国服,打野路国服,中路国服".split(",");
        }else {
            strings = "内测,荣耀属性,星元皮肤,稀有皮肤,史诗传说".split(",");
        }
        String neice = "http://le.sycwy.cn/ajax.php?action=fiedles&type=neice";
        String rongyaoshuijing = "http://le.sycwy.cn/ajax.php?action=fiedles&type=rongyaoshuijing";
        String xingyuanpifu = "http://le.sycwy.cn/ajax.php?action=fiedles&type=xingyuanpifu";
        String xiyoupifu = "http://le.sycwy.cn/ajax.php?action=fiedles&type=xiyoupifu";
        String shishichuanshuo = "http://le.sycwy.cn/ajax.php?action=fiedles&type=shishichuanshuo";
        //国服属性
        String gffy = "http://duoduo.sycwy.cn/ajax.php?action=fiedles_gf&type=gffy";
        String gfdk = "http://duoduo.sycwy.cn/ajax.php?action=fiedles_gf&type=gfdk";
        String gfyz = "http://duoduo.sycwy.cn/ajax.php?action=fiedles_gf&type=gfyz";
        String gfdy = "http://duoduo.sycwy.cn/ajax.php?action=fiedles_gf&type=gfdy";
        String gfzl = "http://duoduo.sycwy.cn/ajax.php?action=fiedles_gf&type=gfzl";

        String shuxing = "";
        List<JSONObject> collect = new ArrayList<>();
        for(String string : strings){
            JSONObject jsonObject = new JSONObject();
            if(string.equals("内测")){
                jsonObject.put("id","neice");
                shuxing = getShuxing(neice);
            }else if(string.equals("荣耀属性")){
                jsonObject.put("id","rongyaoshuijing");
                shuxing = getShuxing(rongyaoshuijing);
            }else if(string.equals("星元皮肤")){
                jsonObject.put("id","xingyuanpifu");
                shuxing = getShuxing(xingyuanpifu);
            }else if(string.equals("稀有皮肤")){
                jsonObject.put("id","xiyoupifu");
                shuxing = getShuxing(xiyoupifu);
            }else if(string.equals("史诗传说")){
                jsonObject.put("id","shishichuanshuo");
                shuxing = getShuxing(shishichuanshuo);
            }else if(string.equals("发育路国服")){
                jsonObject.put("id","gffy");
                shuxing = getShuxing(gffy);
            }else if(string.equals("对抗路国服")){
                jsonObject.put("id","gfdk");
                shuxing = getShuxing(gfdk);
            }else if(string.equals("游走路国服")){
                jsonObject.put("id","gfyz");
                shuxing = getShuxing(gfyz);
            }else if(string.equals("打野路国服")){
                jsonObject.put("id","gfdy");
                shuxing = getShuxing(gfdy);
            }else if(string.equals("中路国服")){
                jsonObject.put("id","gfzl");
                shuxing = getShuxing(gfzl);
            }
            jsonObject.put("name",string);
            jsonObject.put("value",shuxing);
            collect.add(jsonObject);
        }

        List<InterAttrRes> interAttrResList = new ArrayList<>();
        for (JSONObject object : collect) {
            String string = object.getString("value");
            JSONArray jsonArray = JSONArray.parseArray(string);
            InterAttrRes interAttrRes = new InterAttrRes();
            interAttrRes.setId(object.getString("id"));
            interAttrRes.setName(object.getString("name"));
            List<InterAttrRes> interAttrResList1 = new ArrayList<>();
            for(Object o : jsonArray) {
                InterAttrRes interAttrRes1 = new InterAttrRes();
                interAttrRes1.setId(JSONObject.parseObject(o.toString()).getString("id"));
                interAttrRes1.setName(JSONObject.parseObject(o.toString()).getString("name"));
                interAttrResList1.add(interAttrRes1);
                interAttrRes.setInterServerRes(interAttrResList1);
            }
            interAttrResList.add(interAttrRes);
        }
        return interAttrResList;
    }

    static String getShuxing(String url) {
        Map<String, Object> params = new TreeMap<>();
        String result = HttpUtils.doPostForm(url, params, null);
        return result;
    }

}